{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy import stats\n",
    "import numpy as np\n",
    "# 导入pyreadstat包，pandas也使用这个包来打开商业分析软件（如SPSS、STATA）的文件。\n",
    "# 在<https://github.com/Roche/pyreadstat>可查阅该包的详细文档。\n",
    "import pyreadstat,pandas\n",
    "\n",
    "# pyreadstat.read_sav方法有几个重要参数：\n",
    "# apply_value_formats 默认为False，如果我们想要标签描述（即变量中的选项名称，如喜欢、不喜欢），而不仅仅是数字的话（即变量在SPSS中的数值，通常为1、2……），需要将这个参数设置为True。\n",
    "# formats_as_category 默认为True, 意味着读入到Pandas时会将变量转化为category类型的列。\n",
    "# formats_as_ordered_category 默认为False，需要将其设置为True，这样pandas在读取时，会保留在SPSS中定义的变量测量层次。\n",
    "# 该函数返回的值是一个元组，第一个元素为DataFrame类型，第二个元素为整个表变量的定义信息。\n",
    "df, metadata = pyreadstat.pyreadstat.read_sav(r'identity.sav',apply_value_formats=True,formats_as_ordered_category=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 905 entries, 0 to 904\n",
      "Data columns (total 28 columns):\n",
      " #   Column                 Non-Null Count  Dtype   \n",
      "---  ------                 --------------  -----   \n",
      " 0   问卷编号                   867 non-null    float64 \n",
      " 1   调查员                    905 non-null    object  \n",
      " 2   民族                     865 non-null    category\n",
      " 3   政治面貌                   865 non-null    category\n",
      " 4   年级                     864 non-null    category\n",
      " 5   您觉得自己是个典型的中国人吗         865 non-null    category\n",
      " 6   与世界其他国家的人相比中国人有自己的特点吗  864 non-null    category\n",
      " 7   v1                     865 non-null    category\n",
      " 8   v2                     865 non-null    category\n",
      " 9   v3                     865 non-null    category\n",
      " 10  v4                     865 non-null    category\n",
      " 11  你是否了解重活民族的传统节日         865 non-null    category\n",
      " 12  v5                     864 non-null    category\n",
      " 13  您觉得中国怎么样               865 non-null    category\n",
      " 14  您认为中国有多少值得自豪的地方        865 non-null    category\n",
      " 15  您认为世界有多少比例的人尊重中国       867 non-null    category\n",
      " 16  对您而言作为一名中国人有多重要        868 non-null    category\n",
      " 17  会以中国人自豪吗               868 non-null    category\n",
      " 18  会隐瞒身份吗                 868 non-null    category\n",
      " 19  会打多少分                  868 non-null    category\n",
      " 20  国歌升起                   867 non-null    category\n",
      " 21  世博会                    867 non-null    category\n",
      " 22  中国传统文化                 868 non-null    category\n",
      " 23  发展信心                   867 non-null    category\n",
      " 24  你会为中国运动员呐喊助威           867 non-null    category\n",
      " 25  遇到灾难时中国人应该伸出援手         867 non-null    category\n",
      " 26  你愿意加入其他国籍吗             867 non-null    category\n",
      " 27  中国人要为祖国统一奋斗吗           866 non-null    category\n",
      "dtypes: category(26), float64(1), object(1)\n",
      "memory usage: 44.8+ KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        会\n",
       "1        会\n",
       "2       有些\n",
       "3        会\n",
       "4        会\n",
       "      ... \n",
       "900     有些\n",
       "901     有些\n",
       "902    无所谓\n",
       "903      会\n",
       "904    无所谓\n",
       "Name: 会以中国人自豪吗, Length: 905, dtype: category\n",
       "Categories (5, object): ['不会' < '不太会' < '无所谓' < '有些' < '会']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['会以中国人自豪吗']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 找到变量'会以中国人自豪吗'等于'不会'的个案\n",
    "# df[df['会以中国人自豪吗'] == '不会']\n",
    "# 删除完全空行\n",
    "df = df.dropna(how=\"all\", axis=0)\n",
    "# 保留至少有 n 个非 NA 数的行\n",
    "df = df.dropna(thresh=14)\n",
    "# 标记重复行\n",
    "df.insert(1, '是否重复', df.duplicated(subset=['问卷编号']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.rcParams[\"font.sans-serif\"]=[\"SimHei\"] #设置字体\n",
    "plt.rcParams[\"axes.unicode_minus\"]=False #该语句解决图像中的“-”负号的乱码问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:xlabel='年级'>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXMAAAEQCAYAAABC2pRmAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAARnElEQVR4nO3df4xlZX3H8ffHXdB111oIk4U14rYVtf7aolsEusD6gypCaSFWLLZNI3VTrVqrScVqTGjUUNNYFSO6DaFI1ZZCtVRECwIFXEV3W0FrsKa6KIjt2Cpb/A18+8c5OLOzMzuX2bv3Ds99v5KbOfc5d8588+TO5z73Ob9SVUiSHtweMu4CJEn7zjCXpAYY5pLUAMNckhpgmEtSAwxzSWrAynH80UMOOaTWr18/jj8tSQ9aO3bs+HZVTc23bixhvn79erZv3z6OPy1JD1pJbltondMsktQAw1ySGmCYS1IDDHNJaoBhLkkNMMwlqQGGuSQ1wDCXpAaM5aShYVh/9hXjLgGAneeePO4SJGmwkXmSg5OcmOSQ/V2QJOmBWzTMkxwGXAEcBVybZCrJBUm2JXnjrNft0SZJGo1BRuZPAv64qt4CfAJ4FrCiqo4F1iU5Isnpc9v2X8mSpLkWnTOvqqsBkhxPNzo/GLikX30NsAk4cp62rwy7WEnS/AadMw9wBvATIMAd/apdwFp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fEqKrzgfNntyW5qd8Rev/zY+huH/dJukvAXgf8kO6SsPfR3andMNeyY5hr0u124aiq+jTwzCQHAa+l2yH6D8BbgVuAv0lyaFX9/cgrlfbCaRZpL5KspLtq3n2z2pbdtawlw1ySGuDRLJLUAMNckhpgmEtSAwxzSWqAYS5JDfh/yz47GdlWugIAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 单变量分析\n",
    "df.value_counts('年级').plot(kind='bar')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas_profiling as pp\n",
    "report = pp.ProfileReport(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "report"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>年级</th>\n",
       "      <th>预科</th>\n",
       "      <th>大一</th>\n",
       "      <th>大二</th>\n",
       "      <th>大三</th>\n",
       "      <th>大四</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>政治面貌</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>团员</th>\n",
       "      <td>0.020472</td>\n",
       "      <td>0.409449</td>\n",
       "      <td>0.327559</td>\n",
       "      <td>0.148031</td>\n",
       "      <td>0.094488</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>党员</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.132743</td>\n",
       "      <td>0.292035</td>\n",
       "      <td>0.389381</td>\n",
       "      <td>0.185841</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>群众</th>\n",
       "      <td>0.010526</td>\n",
       "      <td>0.294737</td>\n",
       "      <td>0.305263</td>\n",
       "      <td>0.273684</td>\n",
       "      <td>0.115789</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>其他</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.190476</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.380952</td>\n",
       "      <td>0.095238</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>All</th>\n",
       "      <td>0.016204</td>\n",
       "      <td>0.355324</td>\n",
       "      <td>0.320602</td>\n",
       "      <td>0.199074</td>\n",
       "      <td>0.108796</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "年级          预科        大一        大二        大三        大四\n",
       "政治面貌                                                  \n",
       "团员    0.020472  0.409449  0.327559  0.148031  0.094488\n",
       "党员    0.000000  0.132743  0.292035  0.389381  0.185841\n",
       "群众    0.010526  0.294737  0.305263  0.273684  0.115789\n",
       "其他    0.000000  0.190476  0.333333  0.380952  0.095238\n",
       "All   0.016204  0.355324  0.320602  0.199074  0.108796"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pandas.crosstab(df['政治面貌'],df['年级'], normalize='index',margins=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1, metadata = pyreadstat.pyreadstat.read_sav(r'identity.sav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>年级</th>\n",
       "      <th>政治面貌</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>年级</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.199573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>政治面貌</th>\n",
       "      <td>0.199573</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            年级      政治面貌\n",
       "年级    1.000000  0.199573\n",
       "政治面貌  0.199573  1.000000"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1[['年级','政治面貌']].corr(method='kendall')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "chi2, p, dof, ex = stats.chi2_contingency(pandas.crosstab(df['年级'], df['政治面貌']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "69.56857728162603 0.00\n"
     ]
    }
   ],
   "source": [
    "print(chi2,f'{p:0.2f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 告诉我结论(p):\n",
    "  if p<=0.05:\n",
    "    return f'p值为{p:0.2f}，小于0.05，拒绝虚无假设。'\n",
    "  return f'p值为{p:0.2f}，大于0.05，接受虚无假设。'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "69.56857728162603 p值为0.00，小于0.05，拒绝虚无假设。\n"
     ]
    }
   ],
   "source": [
    "print(chi2,告诉我结论(p))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>年级</th>\n",
       "      <th>您觉得自己是个典型的中国人吗</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>年级</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.042375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>您觉得自己是个典型的中国人吗</th>\n",
       "      <td>-0.042375</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      年级  您觉得自己是个典型的中国人吗\n",
       "年级              1.000000       -0.042375\n",
       "您觉得自己是个典型的中国人吗 -0.042375        1.000000"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1[['年级','您觉得自己是个典型的中国人吗']].corr(method='kendall')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "somersdy = stats.somersd(pandas.crosstab(df['年级'], df['您觉得自己是个典型的中国人吗']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SomersDResult(statistic=-0.038468845338627905, pvalue=0.1750803371779197, table=array([[  0,   0,   8,   1,   5],\n",
      "       [ 15,  14,  45,  75, 158],\n",
      "       [  8,  26,  37,  81, 125],\n",
      "       [  0,  12,  31,  68,  61],\n",
      "       [  3,   5,  17,  24,  45]], dtype=int64))\n"
     ]
    }
   ],
   "source": [
    "print(somersdy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "相关系数somersd的值为-0.04,p值为0.18，大于0.05，接受虚无假设。\n"
     ]
    }
   ],
   "source": [
    "print(f'相关系数somersd的值为{somersdy.statistic:0.2f},{告诉我结论(somersdy.pvalue)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "def correlation_ratio(categories, values):\n",
    "    \"\"\"\n",
    "    计算定类变量与定距变量之间相关系数eta\n",
    "\n",
    "    参数：\n",
    "    categories：为定类变量\n",
    "    values：为定距变量\n",
    "\n",
    "    返回值：eta\n",
    "    \"\"\"\n",
    "    cat = np.unique(categories, return_inverse=True)[1]\n",
    "    values = np.array(values)\n",
    "\n",
    "    ssw = 0\n",
    "    ssb = 0\n",
    "    for i in np.unique(cat):\n",
    "        subgroup = values[np.argwhere(cat == i).flatten()]\n",
    "        ssw += np.sum((subgroup - np.mean(subgroup))**2)\n",
    "        ssb += len(subgroup) * (np.mean(subgroup) - np.mean(values))**2\n",
    "\n",
    "    return (ssb / (ssb + ssw))**.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eta_squared: 0.695\n",
      "Eta: 0.834\n"
     ]
    }
   ],
   "source": [
    "df2, metadata = pyreadstat.pyreadstat.read_sav(r'score.sav',apply_value_formats=True)\n",
    "coef = correlation_ratio(df2['职业'], df2['英语成绩'])\n",
    "\n",
    "print(f'Eta_squared: {coef**2:.3f}\\nEta: {coef:.3f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "F_onewayResult(statistic=19.37795183902871, pvalue=4.124236191160844e-05)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.f_oneway(df2[df2['职业'] == '干部'].英语成绩,df2[df2['职业'] == '工人'].英语成绩,df2[df2['职业'] == '农民'].英语成绩)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:xlabel='教育年期', ylabel='家务劳动时间'>"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAEECAYAAADTdnSRAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAVt0lEQVR4nO3df5xddX3n8dfnkmESSZSQDKgEpBrWdpUQ7ZQColUqShWhGHexP8QulmyFXbeyraH6kN2i1Zr6sH1IlZLSdmsVW2pUrNV2oZAutMY6SIhsF1bYIgnrjzFNIGOTYcL97B/njE6yk+TOzD25d768no/HPHLvOfee8zmPk7znm+/5nu+JzESSVJZWrwuQJHWf4S5JBTLcJalAhrskFchwl6QCGe6SVKAFvS4AYPny5XnKKaf0ugxJmlfuvvvu72bm0HTr+iLcTznlFEZGRnpdhiTNKxHxjYOts1tGkgpkuEtSgQx3SSqQ4S5JBTLcJalAjYR7RCyIiEciYlP9c1oT+9kxNs6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H8CPw08BnwaWEY1cdj/AE4ENgPPjIjP1995fkR8gerfz+cy83en7Pt3ImLXlPfPq+/YPaXejnREGO4q0f3Am4GXUXWTvAG4gyrwx6juAp60kqpF/8SUZS+i6rYhMx8GromI24FXZea+ulV+sInEbgUeAH6xfj8K/BLwH+Z+WFLnDHeV6FXA7cB7gY8Ba6lC9kbgdcC7p3x2L1Ug756y7CSq2TmZctF12otT9SRWAVwB/AxVV9CpwA9FxJX1dpdTzSUiHTGGu0q0nSpo30TVHXI18GGqwL8sMw+cO/s89m+5T33U4VrgIqqLsp+tB9A8f0oXzVHAZzLzuoiYoJonZAlwXL3u9sy8KyJe2b3Dkw7PcFeJHgFuoGqVX52ZV0TEeVTz5T9Uj1xZn5n/Un/+39bT4QIQEXdNvs7M6zlg3vCI+PKBF1Trh5ScTzUCZwHV/xJOA94YEcdQ/W/g77p6lNIhOM5dxcnMbwMPU41y+WJE/CpV8L6eqi9+F9X0xFDNxPl9EfFe4KTM3HeIXSyeZtlW4Bcys52ZTwD/BbghMx+gGoWzBy+o6ghynLs0RUQsAvbO9ymXJcNdkgpkt4wkFchwl6QCGe6SVCDDXZIKZLhLUoH+H4xsi/E2zOScAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "df3, metadata = pyreadstat.pyreadstat.read_sav(r'pearson.sav')\n",
    "df3[['教育年期', '家务劳动时间']].plot.scatter(x=\"教育年期\", y=\"家务劳动时间\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "相关系数 r 平方为0.65，显著性 p 值为0.01\n"
     ]
    }
   ],
   "source": [
    "r, p = stats.pearsonr(df3['教育年期'], df3['家务劳动时间'])\n",
    "print(f'相关系数 r 平方为{r**2:.2f}，显著性 p 值为{p:.2f}')"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "11f1dc213e07634baa4c5c321dec03c05dafae643c50f20e6d1a492290c05dc2"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
